package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.project.ProjectSettings;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.onenesspathfinder.controllers.RecitationController;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.quran.QuranicVerse;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;

public class RecitationDialog {

	private static JDialog dialog;
	private static JButton okButton;
	private static JScrollPane scrollPane;
	private static JTextArea textArea;
	private static final JPanel panel = new JPanel();

	private static final JPanel panel_1 = new JPanel();
	private static final JPanel panel_2 = new JPanel();
	private static final JComboBox<String> comboBox = new JComboBox<>();
	private static final JComboBox<Integer> comboBox_1 = new JComboBox<>();
	private static final JComboBox<Integer> comboBox_2 = new JComboBox<>();
	private static final JButton btnNewButton = new JButton(
			ProjectLocalization.getString(
					ProjectConstants.ONENESS_PATHFINDER_PROJECT, "AddSection"));
	private static final JButton btnNewButton_1 = new JButton(
			ProjectLocalization.getString(
					ProjectConstants.ONENESS_PATHFINDER_PROJECT, "AddVerses"));
	private static final JButton btnNewButton_2 = new JButton(
			ProjectLocalization.getString(ProjectConstants.CORE_PROJECT,
					"Clear"));
	private static ArrayList<QuranicVerse> verses = new ArrayList<>();
	private static final JCheckBox checkBox = new JCheckBox(
			ProjectLocalization.getString(
					ProjectConstants.ONENESS_PATHFINDER_PROJECT,
					"ShowNoDialogDuringRecitation"));

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setResizable(false);
		dialog.setIconImage(null);
		dialog.setTitle(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "RecitationList"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setPreferredSize(new Dimension(400, 300));
		dialog.setSize(new Dimension(400, 300));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 0));
		dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);

		buttonPane.add(checkBox);
		okButton = new JButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "OK"));
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		comboBox.setModel(new DefaultComboBoxModel<String>(Quran
				.getSectionNames(ProjectSettings.getLocale().getLanguage())));
		scrollPane = new JScrollPane();
		dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);
		dialog.getContentPane().add(panel, BorderLayout.NORTH);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 294,
						Short.MAX_VALUE)
				.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 294,
						Short.MAX_VALUE));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 32,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 41,
								Short.MAX_VALUE)));
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		panel_2.add(btnNewButton);

		panel_2.add(btnNewButton_1);

		panel_2.add(btnNewButton_2);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		panel_1.add(comboBox);

		panel_1.add(comboBox_1);

		panel_1.add(comboBox_2);
		panel.setLayout(gl_panel);

		textArea = new JTextArea();
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setSelectedTextColor(Color.black);
		textArea.setSelectionColor(Color.white);
		textArea.setOpaque(false);
		scrollPane.setViewportView(textArea);
		initListeners();
	}

	private static void initListeners()
	{

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
							{
								dialog.setVisible(false);
								UIDelegate.setOpenDialog(false);
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

		okButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				QuranicVerse[] v = new QuranicVerse[verses.size()];
				v = verses.toArray(v);
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
				RecitationController.reciteCustom(v, !checkBox.isSelected());
			}
		});

		comboBox_2.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (comboBox_2.getSelectedIndex() < comboBox_1
						.getSelectedIndex())
				{
					comboBox_2.setSelectedIndex(comboBox_1.getSelectedIndex());
				}

			}
		});

		comboBox_1.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (comboBox_2.getSelectedIndex() < comboBox_1
						.getSelectedIndex())
				{
					comboBox_2.setSelectedIndex(comboBox_1.getSelectedIndex());
				}

			}
		});

		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

		comboBox.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				int k = Quran.getInSectionVerseCount(comboBox
						.getSelectedIndex() + 1);
				Integer[] vals = new Integer[k];
				for (int i = 0; i < k; i++)
				{
					vals[i] = i + 1;
				}
				comboBox_1.setModel(new DefaultComboBoxModel<Integer>(vals));
				comboBox_2.setModel(new DefaultComboBoxModel<Integer>(vals));
				comboBox_1.setSelectedIndex(0);
				comboBox_2.setSelectedIndex(0);
			}
		});

		btnNewButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				QuranicVerse[] tmp = Quran.getVerses(comboBox
						.getSelectedIndex() + 1);
				for (int i = 0; i < tmp.length; i++)
				{
					verses.add(tmp[i]);
				}
				textArea.setText(textArea.getText()
						+ Quran.getSectionName(comboBox.getSelectedIndex() + 1,
								ProjectSettings.getLocale().getLanguage())
						+ "\n");
			}
		});

		btnNewButton_1.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				for (int i = comboBox_1.getSelectedIndex() + 1; i <= comboBox_2
						.getSelectedIndex() + 1; i++)
				{
					verses.add(Quran.getVerse(comboBox.getSelectedIndex() + 1,
							i));
				}
				textArea.setText(textArea.getText()
						+ Quran.getVerse(comboBox.getSelectedIndex() + 1,
								comboBox_1.getSelectedIndex() + 1)
								.toLongNotationString() + "/"
						+ (comboBox_2.getSelectedIndex() + 1) + "\n");
			}
		});

		btnNewButton_2.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				verses = new ArrayList<>();
				textArea.setText("");
			}
		});
	}

	private static void refreshValues()
	{
		QuranicVerse v = UIDelegate.getDisplayedVerse();
		comboBox.setSelectedIndex(v.getSectionNumber() - 1);
		comboBox_1.setSelectedIndex(v.getVerseNumber() - 1);
		comboBox_2.setSelectedIndex(v.getVerseNumber() - 1);
	}

	public static void showDialog()
	{
		init();
		refreshValues();
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		UIDelegate.setOpenDialog(true);
		dialog.setVisible(true);
	}
}
